package cn.congzhou.crm.web.controller;


import cn.congzhou.crm.domain.Permission;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.IPermissionService;
import cn.congzhou.crm.utils.ResultJSON;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.Get;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    private Logger logger = LoggerFactory.getLogger(PermissionController.class);

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"查看系统权限","permission:list"})
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {

        model.addAttribute("ro", permissionService.selectByPage(qo));

        return "/permission/list";
    }


    @RequestMapping("/reload")
    @RequiresPermissions(value = {"加载权限","permission:reload"},logical = Logical.OR)
    public String reload() {
        permissionService.reload();

        return "redirect:/permission/list.do";
    }

    //==========================添加ligerUI添加的方法============================================//

    @RequestMapping(value = "toPermissionGrid",method = RequestMethod.GET)
    @RequiresPermissions(value = {"查看系统权限","permission:query"})
    public String toPermissionGrid(){

        return "/permission/permissionGrid";
    }

    @ResponseBody
    @RequestMapping(value = "/getPermissionData",method = RequestMethod.POST)
    @RequiresPermissions(value = {"查看系统权限","permission:query"})
    public Object getPermissionData(@ModelAttribute("qo") QueryObject qo){
        PageInfo<Permission> pageInfo = permissionService.selectByPage(qo);
        return pageInfo;
    }

    @ResponseBody
    @RequestMapping(value = "/reloadPermission",method = RequestMethod.GET)
    @RequiresPermissions(value = {"加载权限","permission:reload"},logical = Logical.OR)
    public Object reloadPermission() {
        ResultJSON result = new ResultJSON();
        try {
            permissionService.reload();
        } catch (Exception e) {
            result.mark("加载系统权限出错");
            logger.error("加载系统权限出错",e);
        }

        return result;
    }


}
